package com.ruoyi.web.controller.applet.controller;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.bean.BeanUtils;
import com.ruoyi.system.domain.SysAppArticle;
import com.ruoyi.system.domain.SysDictData;
import com.ruoyi.system.dto.SysDictDataDTO;
import com.ruoyi.system.service.ISysAppArticleService;
import com.ruoyi.system.service.ISysDictTypeService;

@Controller
@RequestMapping("/applet/article")
public class ArticleController {

	@Autowired
	private ISysAppArticleService sysAppArticleService;

	@Autowired
	private ISysDictTypeService dictTypeService;

	@PostMapping("/getArticleData")
	@ResponseBody
	public AjaxResult getArticleData() {
		Map<String, Object> map = Maps.newConcurrentMap();
		List<SysDictData> dictDatas = dictTypeService.selectDictDataByType("sys_article_sort");
		SysDictDataDTO dictDataDto = null;
		List<SysDictDataDTO> dtos = Lists.newArrayList();
		for (SysDictData dict : dictDatas) {
			dictDataDto = new SysDictDataDTO();
			BeanUtils.copyBeanProp(dictDataDto, dict);
			// 获取分类下的文章
			SysAppArticle sysAppArticle = new SysAppArticle();
			sysAppArticle.setDictCode(dict.getDictCode() + "");
			// sysAppArticle.setNoticeContent(keyword);
			List<SysAppArticle> articles = sysAppArticleService.selectSysAppArticleList(sysAppArticle);
			if(articles.size()>6) {
				dictDataDto.setArticles(articles.subList(0, 6));
			}else {
				dictDataDto.setArticles(articles);
			}
			dtos.add(dictDataDto);
		}
		map.put("sortinfo", dtos);
		return AjaxResult.success("操作成功", map);
	}

	/**
	 * 根据dictCode获取文章列表
	 * 
	 * @Description:
	 * @Author 豫东一棵草
	 * @DateTime 2020年10月25日 上午12:10:10
	 * @return
	 */
	@PostMapping("/getArticleByDictCode")
	@ResponseBody
	public AjaxResult getArticleByDictCode(String dictCode) {
		Map<String, Object> map = Maps.newConcurrentMap();
		SysAppArticle sysAppArticle = new SysAppArticle();
		sysAppArticle.setDictCode(dictCode);
		List<SysAppArticle> articles = sysAppArticleService.selectSysAppArticleList(sysAppArticle);
		map.put("articles", articles);
		return AjaxResult.success("操作成功", map);
	}

	/**
	 * 根据id获取文章的详情
	 * 
	 * @param articleId
	 * @return
	 */
	@PostMapping("/getArticleById")
	@ResponseBody
	public AjaxResult getArticleById(Long articleId) {
		Map<String, Object> map = Maps.newConcurrentMap();
		SysAppArticle article = sysAppArticleService.selectSysAppArticleById(articleId);
		map.put("articleinfo", article);
		return AjaxResult.success("操作成功", map);
	}

}
